/**
 * 类名：HttpResult
 * @author hmb
 * 创建日期： 2014-10-1 上午12:10:43
 *
 * 备注：封装了httpclients请求得到的结果
 * 
 */
package com.erms.util.http;

import java.io.IOException;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

import com.erms.util.LogUtil;

/**
 * @author hmb
 *
 */
public class HttpResult {
	private static final String CHARSET_UTF8 = "UTF-8";
	private static final String CHARSET_GBK = "GBK";

	private String content="";
	private StatusLine statusLine;
	private Locale locale;
	private String cookie;
	private String charset;
	private String location;

	public HttpResult() {
		// TODO 自动生成的构造函数存根
	}
	public HttpResult(CloseableHttpResponse response) {
		statusLine = response.getStatusLine();
		locale = response.getLocale();
		cookie = response.getFirstHeader("Set-Cookie") == null
				? null
						: response.getFirstHeader("Set-Cookie").getValue();
		location = response.getFirstHeader("Location") == null
				? null
						: response.getFirstHeader("Location").getValue();
		HttpEntity entity = response.getEntity();
		charset = EntityUtils.getContentCharSet(entity);
		try {
			content = EntityUtils.toString(entity, charset == null
					? CHARSET_UTF8
							: charset);
		} catch (ParseException e) {
			LogUtil.error("不能成功读取HttpClient请求结果", e);
		} catch (IOException e) {
			LogUtil.error("不能成功读取HttpClient请求结果", e);
		}
	}
	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		// TODO 自动生成的方法存根
		String s = "";
		s += "\nstatus line: "+statusLine;
		s += "\nlocale: "+locale;
		s += "\nlocation: "+ location;
		s += "\ncookie: " + cookie;
		s += "\ncharset: " + charset;
		s += "\ncontent: " + content;
		return s;
	}

	/**
	 * @return content
	 */
	public String getContent() {
		return content;
	}
	/**
	 * @param content
	 */
	public void setContent(String content) {
		this.content = content;
	}
	/**
	 * @return statusLine
	 */
	public StatusLine getStatusLine() {
		return statusLine;
	}
	/**
	 * @param statusLine
	 */
	public void setStatusLine(StatusLine statusLine) {
		this.statusLine = statusLine;
	}
	/**
	 * @return locale
	 */
	public Locale getLocale() {
		return locale;
	}
	/**
	 * @param locale
	 */
	public void setLocale(Locale locale) {
		this.locale = locale;
	}
	/**
	 * @return cookie
	 */
	public String getCookie() {
		return cookie;
	}
	/**
	 * @param cookie
	 */
	public void setCookie(String cookie) {
		this.cookie = cookie;
	}
	/**
	 * @return charset
	 */
	public String getCharset() {
		return charset;
	}
	/**
	 * @param charset
	 */
	public void setCharset(String charset) {
		this.charset = charset;
	}
	/**
	 * @return location
	 */
	public String getLocation() {
		return location;
	}
	/**
	 * @param location
	 */
	public void setLocation(String location) {
		this.location = location;
	}

}
